<?php
class Info_planAction extends IntesysAction {
	function index(){
        //筛选结果并显示
		$this->initView();
        $this->dao=D('Info');
		$this->dao->filter('plan',$this->view);
	   
       //获取 筛选或添加 所需的积分规则类型，积分类型，部门，所有员工等数据
       $membergroup=D('Member_group')->getGroup();
       $this->assign('grouplist',$membergroup);
       $staff=D('Member')->getallgroupmember();
       $this->assign('memberlist',$staff);	
       if($_POST['requestType']==10011011){
           die(json_encode(array('plan'=>$list,'statu'=>1)));
       }else{
           $this->display();    
       }
	   
	}
	public function update(){
        $this->dao=$this->dao = D('Info');
		$info_itemDB=M('Info_item');
		if($this->_get('id')){
            $_POST['id']=$this->_get('id');
            if($this->_get('statu')){
                $_POST['status']=$this->_get('statu');
            }
            if(isset($_POST['starttime']))$_POST['starttime']=strtotime($_POST['starttime'].$_POST['starthour']);
            if(isset($_POST['endtime']))$_POST['endtime']=strtotime($_POST['endtime'].$_POST['endhour']);
            if(isset($_POST['realtime']))$_POST['realtime']=strtotime($_POST['realtime'].$_POST['realhour']);
			//先删除该记录的所有详细信息，再循环插入新的详细信息
			$info_itemDB->where(['info_id'=>$this->_get('id')])->delete();
			$data['status']=3;
			$data['time']=time();
			$data['info_id']=$this->_get('id');
			foreach($_POST['item'] as $vo){
				if(''==trim($vo)){continue;}
				$data['content']=$vo;
				$info_itemDB->add($data);
			}
			unset($data,$_POST['item']);
			parent::update();
		}
	}
	
	public function insert(){
        if(IS_POST){
            if(!isset($_POST['type'])){$_POST['type']='plan';}
            $this->dao=$this->dao = D('Info');
			$info_itemDB=M('Info_item');
            $_POST['site_id'] =__SITEID__;
            $_POST['datetime']=time();
            $_POST['starttime']=strtotime($_POST['starttime'].$_POST['starthour']);
            $_POST['endtime']=strtotime($_POST['endtime'].$_POST['endhour']);
            $_POST['member_id']=$_SESSION[C('USER_AUTH_KEY')];
            $_POST['status']=3;
            //$_POST['content']=str_replace(array("\r\n","\n","\r"),'<br/>',strip_tags($_POST['content']));
            //parent::insert();
			if($jumpUrl == ''){
				$jumpUrl =  U('Intesys/' . MODULE_NAME . '/index');
			}
			if ($this->dao->create()) {
				$result = $this->dao->add();
				if (false !== $result) {
					$data['info_id']=$result;
					$data['status']=3;
					$data['time']=time();
					foreach($_POST['item'] as $vo){
						if(''==trim($vo)){continue;}
						$data['content']=$vo;
						$info_itemDB->add($data);
					}
					$this->success('操作成功', $jumpUrl);
				} else {
					echo '{"info":'+$this->dao->getError()+',"status":0}';
					$this->error($this->dao->getError());
				}
			} else {
				echo '{"info":'+$this->dao->getError()+',"status":0}';
				$this->error($this->dao->getError());
			}
			//将工作计划的详细条目循环添加至‘信息详细表’中
        }
	}
	public function delete(){
		if($this->_get('id')){
            $this->dao=$this->dao = D('Info');
			M('Info_item')->where(['info_id'=>$this->_get('id')])->delete();
			parent::delete();
		}
	}
	public function getitems(){
		if($this->_get('id')){
			$info_item=M('Info_item');
			$items=$info_item->where(['info_id'=>$this->_get('id')])->getField('content',true);
			$items=array_reverse($items);
			die(json_encode(['statu'=>1,'items'=>$items]));
		}	
	}
    public function getinfo(){
        if($this->_get('id')){
            $info=D('Info')->getinfo($this->_get('id'));
			$items=D('Info_item')->where(['info_id'=>$this->_get('id')])->getField('content',true);
            die(json_encode(array('info'=>$info,'statu'=>1,'items'=>$items)));    
        }    
    }
}

